package com.example.campuslink.viewModel.userOperation

import android.util.Log
import android.widget.Toast
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.example.campuslink.commen.db.userInfo.UserInfoTable
import com.example.campuslink.commen.db.userNumber.UserNumberTable
import com.example.campuslink.commen.tools.Sp
import com.example.campuslink.commen.tools.SpKey
import com.example.campuslink.viewModel.BaseViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class UserInfoViewModel : BaseViewModel() {
    private val _userInfo = MutableLiveData<UserInfoTable?>()
    val userInfo get() = _userInfo
    private val _imagePathLiveData = MutableLiveData<String>()
    val imagePathLiveData = _imagePathLiveData
    var userNumberInfo: UserNumberTable? = null
    private val _showPassword = MutableLiveData<Boolean>(false)
    private val _againShowPassword = MutableLiveData<Boolean>(false)
    val againShowPassword: MutableLiveData<Boolean> = _againShowPassword
    val showPassword: MutableLiveData<Boolean> = _showPassword
    private val _currentPosition = MutableLiveData<Int>(0)
    private val _againCurrentPosition = MutableLiveData<Int>(0)
    val currentPosition = _currentPosition
    val againCurrentPosition = _againCurrentPosition
    fun setShowPassword(
    ) {
        _showPassword.value = !(_showPassword.value ?: false)
    }

    fun setAgainShowPassword(
    ) {
        _againShowPassword.value = !(_againShowPassword.value ?: false)
    }

    fun setCurrentPosition(index: Int) {
        _currentPosition.value = index
    }

    fun setAgainCurrentPosition(index: Int) {
        _againCurrentPosition.value = index
    }

    fun setImagePath(imagePath: String) {
        _imagePathLiveData.value = imagePath
    }

    fun finish() {
        Toast.makeText(context, "修改成功", Toast.LENGTH_SHORT).show()
        finishActivity()
    }

    fun getUserNumber() {
        viewModelScope.launch(Dispatchers.IO) {
            val userNumber = Sp.getString(context, "userNumber", "")
            val tempUserNumber = userNumberDao.getUserNumber(userNumber ?: return@launch)
            if (tempUserNumber != null) {
                userNumberInfo = tempUserNumber
            }
        }
    }

    fun getUserInfo() {
        viewModelScope.launch(Dispatchers.IO) {
            val userNumber =
                Sp.getString(context ?: return@launch, "userNumber", "") ?: return@launch
            val tempUserInfo = userInfoDao.getUserInfoByUserNumber(userNumber)

            if (tempUserInfo != null) {
                _userInfo.postValue(tempUserInfo)
            }
        }
    }

    fun updateUserInfo(userInfoTable: UserInfoTable) {
        viewModelScope.launch(Dispatchers.IO) {
//            var useTable = userNumberDao.getUserNumber(
//                Sp.getString(
//                    context ?: return@launch,
//                    SpKey.USER_NUMBER.key ?: "",
//                    ""
//                ) ?: return@launch
//            )
//            useTable?.apply {
//                userInfoData = userInfoTable
//            }
//            userNumberDao.updateUserNumber(useTable ?: return@launch)
            userInfoDao.updateUserInfo(userInfoTable)
        }
    }

    fun updateUserNumber(userNumberTable: UserNumberTable) {
        viewModelScope.launch(Dispatchers.IO) {
            userNumberDao.updateUserNumber(userNumberTable)
        }
    }
}